package com.dwjm.client;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    private ConsumerDao consumerDao = new ConsumerDaoImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        Consumer consumer = new Consumer();
        consumer.setAccount(req.getParameter("account"));
        consumer.setPassword1(req.getParameter("password1"));
        consumer.setRealname(req.getParameter("realname"));
        consumer.setIdcard(req.getParameter("idcard"));
        consumer.setTel(req.getParameter("tel"));
        consumer.setAge(Integer.parseInt(req.getParameter("age")));
        consumer.setProvince(req.getParameter("province"));
        consumer.setCity(req.getParameter("city"));
        consumer.setDistrict(req.getParameter("district"));
        consumer.setAddr(req.getParameter("addr"));

        boolean success = consumerDao.register(consumer);

        resp.setContentType("application/json;charset=UTF-8");
        resp.getWriter().write("{\"code\": " + (success ? 200 : 500) + ", \"msg\": \"" + (success ? "注册成功" : "注册失败") + "\"}");
    }
}
